package com.fdb.efp.esb.service.impl.pls;

import com.alibaba.fastjson.JSONObject;
import com.fdb.efp.esb.common.client.resp.EsbRespServiceBean;
import com.fdb.efp.esb.common.constant.ESBClientConstance;
import com.fdb.efp.esb.common.constant.EsbBizEnums;
import com.fdb.basic.framework.core.base.FrameworkService;
import com.fdb.basic.framework.core.constant.ESBConstant;
import com.fdb.efp.esb.service.bo.req.pls.Pls1001ReqBean;
import com.fdb.efp.esb.service.bo.resp.pls.Pls1001RespBean;
import com.fdb.efp.esb.service.client.ESBRequestBuilder;
import com.fdb.efp.esb.service.client.EsbBaseBean;
import com.fdb.efp.esb.service.facade.pls.Pls1001Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;


/**
 * 交易码	PL1001
 * 服务名称	30230003(贷款基本信息查询)
 * 交易名称	借据信息查询
 * 服务场景名称	12(个贷借据信息查询)
 * 交易描述
 * 服务描述	本服务适用于查询贷款申请相关的额度、利率、借据等基本信息查询，不涉及资金交易
 * 服务场景描述	本场景用于向个贷查询贷款借据信息
 */
@Service("pls1001Service")
public class PLs1001ServiceImpl implements Pls1001Service, FrameworkService {

    private static Logger logger = LoggerFactory.getLogger(PLs1001ServiceImpl.class);

    @Override
    public Pls1001RespBean excuteQueryPL1001(Pls1001ReqBean pls1001ReqBean, String golSerno) throws Exception {
        logger.info("======>调用个贷借据查询接口【{}_30230003_12】开始<======", pls1001ReqBean.getCertNo());
        Pls1001RespBean respBean = null;
        try {
            EsbBaseBean esbBaseBean = EsbBaseBean.EsbBaseBeanBuilder.
                    anEsbBaseBean().
                    withINSTID(ESBClientConstance.bdInstId).withGLBLSRLNO(golSerno).build();
            EsbRespServiceBean recvBean = ESBRequestBuilder.buildClient().
                    withEsbBaseBean(esbBaseBean)
                    .withBody(pls1001ReqBean)
                    .withTradeNo(ESBConstant.TRADE_CODE_30230003)
                    .withScene("12")
                    .create()
                    .sendAndReceive();
            if (EsbBizEnums.RetCodeEnum.SUCCESS.VALUE.equals(recvBean.getSysHead().getRetInfArry().get(0).getRetCd()) ||
                    "0000".equals(recvBean.getSysHead().getRetInfArry().get(0).getRetCd()) ||
                    "9999".equals(recvBean.getSysHead().getRetInfArry().get(0).getRetCd())) {
                respBean = recvBean.getBody(Pls1001RespBean.class);
            } else {
                throw new Exception(recvBean.getSysHead().getRetInfArry().get(0).getRetMsg());
            }
            logger.info("调用个贷借据查询接口【30230003_12】结束，返回信息：" + JSONObject.toJSONString(respBean));
        } catch (Exception e) {
            logger.error("调用个贷借据查询接口【30230003_12】出现异常：{}", e.getMessage());
            throw new Exception("调用个贷借据查询接口【30230003_12】出现异常：" + e.getMessage());
        } finally {
            logger.info("======>调用个贷借据查询接口【{}_30230003_12】结束<======", pls1001ReqBean.getCertNo());
        }
        return respBean;
    }
}
